package logic

import (
	"context"
	"strings"

	"mall/service/backend/model"
	"mall/service/backend/rpc/internal/svc"
	"mall/service/backend/rpc/types/backend"

	"github.com/zeromicro/go-zero/core/logx"
)

type MemberLevelListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMemberLevelListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MemberLevelListLogic {
	return &MemberLevelListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *MemberLevelListLogic) MemberLevelList(in *backend.MemberLevelListReq) (*backend.MemberLevelListResp, error) {
	var orderBys []string
	if len(in.OrderBy) > 0 {
		for _, orderBy := range in.OrderBy {
			var order string = " ASC"
			if orderBy.Order == "1" {
				order = " DESC"
			}

			if orderBy.Field == "id" {
				orderBys = append(orderBys, model.MemberLevelColumns.MemberLevelID+order)
			}
		}
	}

	filterMap := make(map[string]map[string][]interface{})
	if in.Name != "" {
		filterMap[model.MemberLevelColumns.Name] = map[string][]interface{}{
			"startWithStrict": []interface{}{in.Name},
		}
	}
	if in.Slug != "" {
		filterMap[model.MemberLevelColumns.Slug] = map[string][]interface{}{
			"startWithStrict": []interface{}{in.Slug},
		}
	}

	list, count, err := l.svcCtx.MemberLevel.GetList(l.ctx, model.M{
		"page":     in.Current,
		"pageSize": in.PageSize,
		"orderBy":  strings.Join(orderBys, ","),
	}, 1, filterMap)
	if err != nil {
		return nil, err
	}

	var mls []*backend.MemberLevelInfo
	if len(list) > 0 {
		for _, item := range list {
			var (
				withMemberPrice int32
				withPromotion   int32
				withFreeFreight int32
				withComment     int32
				withSignIn      int32
				withBirthday    int32
				isDefault       int32
			)
			if item.PrivilegeMemberPrice > 0 {
				withMemberPrice = 1
			}
			if item.PrivilegePromotion > 0 {
				withPromotion = 1
			}
			if item.PrivilegeFreeFreight > 0 {
				withFreeFreight = 1
			}
			if item.PrivilegeComment > 0 {
				withComment = 1
			}
			if item.PrivilegeSignIn > 0 {
				withSignIn = 1
			}
			if item.PrivilegeBirthday > 0 {
				withBirthday = 1
			}
			if item.IsDefault > 0 {
				isDefault = 1
			}
			mls = append(mls, &backend.MemberLevelInfo{
				Id:                 int64(item.MemberLevelID),
				Name:               item.Name,
				Slug:               item.Slug,
				Desc:               item.Description,
				MediaId:            int64(item.ThumbnailID),
				MediaUrl:           item.ThumbnailURL,
				Integral:           int32(item.GrowthThreshold),
				WithMemberPrice:    withMemberPrice,
				WithPromotion:      withPromotion,
				WithFreeFreight:    withFreeFreight,
				WithComment:        withComment,
				WithSignIn:         withSignIn,
				WithBirthday:       withBirthday,
				FreeFreight:        int32(item.FreeFreightThreshold),
				IntegralForComment: int32(item.GrowthPerComment),
				TaskCount:          int32(item.TaskCount),
				IsDefault:          isDefault,
			})
		}
	}

	return &backend.MemberLevelListResp{
		Total: count,
		List:  mls,
	}, nil
}
